(function () {
	//新建账号，打开弹窗
	jQuery('.addBtn').on('click', function () {
		jQuery('#id').val('');
		jQuery('#itemOrder').val('');
		jQuery('#group').val('');
		jQuery('#title').val('');
		jQuery('#value').val('');
		jQuery('#extended1').val('');
		jQuery('#optionModal').modal('show');
	});

    ///编辑和新建保存
    jQuery('.saveBtn').on('click', function () {
    	if (jQuery("#optionEditForm") && !jQuery("#optionEditForm").Validform({
			tiptype : 3,
			showAllError : true
		}).check(false)) {
			return;
		};
		    var id = jQuery('#id').val();
		    var createTime = jQuery('#createTime').val();
		    var group = jQuery('#group').val();
	        var title = jQuery('#title').val();
	        var value=jQuery('#value').val();
	        var itemOrder = jQuery('#itemOrder').val();
	        var extended1 = jQuery('#extended1').val();
	        var url = window.rootPath + "/ui/option/save";
	        var d;
	        if(id==''){
	        	  d = {
	        			"group": group,
     	            "title": title,
     	            "value": value,
     	            "itemOrder": itemOrder,
     	            "extended1":extended1
     	        };
	        }else{
	        		d = {
     	            "id":id,
     	            "createTime":createTime,
     	            "group": group,
     	            "title": title,
     	            "value": value,
     	            "itemOrder": itemOrder,
     	            "extended1": extended1
     	        };
	        	
	        }
	        $("#right").mLoading("show");
	        jQuery.ajax({
	            type: 'post',
	            url: encodeURI(url),
	            data: JSON.stringify(d),
	            dataType: 'json',
	            contentType: 'application/json;charset=utf-8',
	            success: function (data) {
	            		jQuery('#optionModal').modal('hide');
	                serverTable.draw(false);
	                message("选择项信息保存成功！");
	                $("#right").mLoading("hide");
	            }
	        });
    });

    var serverData = {};
    var serverTable;
    jQuery(document).ready(function () {
        serverTable = jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/option/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                data: null
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                        var operate ='<button type="button" class="btn btn-sm btn-info editBtn" title="编辑"><i class="fa fa-pencil"></i></button>'
                            +'<button type="button" class="btn btn-sm btn-info delBtn" title="删除"><i class="fa fa-remove"></i></button>';
                        return operate;

                    }
                },
                {
                    data: 'itemOrder'
                },
                {
                    data: 'group'
                },
                {
                    data: 'title'
                },
                {
                    data: 'value'
                },
                {
                	data: 'extended1'
                }  
              ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }

                serverTable.off('click', 'tr');
                serverTable.on('click', 'tr', function (event) {
                    event = event || window.event;
                    var target = event.target || event.scrElement;
                    var data = serverTable.row(this).data();
                    if (target.className.indexOf('fa-remove') != -1 || target.className.indexOf('delBtn') != -1) {
                        deleteOption(data);
                    }
                    if (target.className.indexOf('fa-pencil') != -1 || target.className.indexOf('editBtn') != -1) {
                        editOption(data);
                    }
                    return;
                });
            }
        });
        var deleteOption = function (data) {
            if (!data) return;
            $.springLemonDialog({
                content: '确认删除' +data.group+'的'+ data.title + '？',//确认文本
                clickButton: function (sender, modal, index) {//按钮事件处理
                    //index:0 确认    1：取消
                    //sender:事件源 如按钮，sender.html()可得到按钮文字如：取消
                    if (index == 0) {
                        var url =window.rootPath+ "/api/options/" + data.id;
                        $("#right").mLoading("show");
                        jQuery.ajax({
                            url: encodeURI(url),
                            cache: false,
                            type: "delete",
                            dataType: "json",
                            success: function (data) {
                                serverTable.draw(false);
                                message('选择项信息删除成功！')
                                $("#right").mLoading("hide");
                            }
                        });
                    }
                    $(this).closeDialog(modal);
                }

            });
        };
        var editOption=function(data){
        	if(!data) return;
        	jQuery('#id').val(data.id);
        	jQuery('#createTime').val(data.createTime)
    		jQuery('#group').val(data.group);
    		jQuery('#title').val(data.title);
    		jQuery('#value').val(data.value);
    		jQuery('#itemOrder').val(data.itemOrder);
    		jQuery('#extended1').val(data.extended1);
    		jQuery('#optionModal').modal('show');
        }
    });
    //查询
    jQuery('.searchBtn').on('click', function() {
		serverData.key = jQuery('#keyword').val();
		serverTable.draw();
	})
	// 撤销查询
	jQuery('.cancelBtn').on("click", function() {
		jQuery('#keyword').val('');
		serverData.key = '';
		serverTable.draw();
	});
})();